<?php
namespace app\tools\logic;
use think\facade\Db;

class MenuLogic{
	static function getList(){
		$parent_id = input('parent_id', 0);
		if($parent_id!=null){
			$list = Db::name('auth_menu')
					->where('parent_id', $parent_id)
					->where('status','<>',127)
					->order('sort asc, id asc')
					->select();
			return $list;
		}else{
			$list = Db::name('auth_menu')
					->where('status','<>',127)
					->order('sort', 'asc, id asc')
					->select();


			$lists = getMenuTree($list,'parent_id',0,0,1,0,'children');
			return $lists;
		}
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('auth_menu')
					->where('id', $id)
					->find();
		return $info;

	}

	static function save(){
		$id = input('id');
		$data = [];
		$data['menu_name'] = input('menu_name','');
		$data['url'] = input('url', '');
		$data['sort'] = input('sort', 50);
		$data['status'] = input('status', 1);
		$data['is_show'] = input('is_show', 1);
		$data['icon'] = input('icon', '');
		$data['parent_id'] = input('parent_id', 0);

		// print_r($data);
		if(!$id){
			return 110;
		}
		if(!$data['menu_name']){
			return 118;
		}


		$rs = Db::name('auth_menu')
				->where('id', $id)
				->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function add(){
		$data = [];
		$data['menu_name'] = input('menu_name','');
		$data['url'] = input('url', '');
		$data['sort'] = input('sort', 50);
		$data['status'] = input('status', 1);
		$data['is_show'] = input('is_show', 1);
		$data['icon'] = input('icon', '');
		$data['parent_id'] = input('parent_id', 0);
		$data['add_time'] = date('Y-m-d H:i:s');

		if(!$data['menu_name']){
			return 118;
		}

		$rs = Db::name('auth_menu')->insert($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('auth_menu')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}
}